package com.springboot.chongwu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.springboot.chongwu.entity.Baike;
import com.springboot.chongwu.entity.Pet;
import com.springboot.chongwu.mapper.PetMapper;
import com.springboot.chongwu.qpage.QPage;
import com.springboot.chongwu.service.IPetService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.springboot.chongwu.util.ResultView;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 宠物表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-09-25
 */
@Service
public class PetServiceImpl extends ServiceImpl<PetMapper, Pet> implements IPetService {

    @Override
    public ResultView getPageList(QPage qPage, Pet obj) {
        IPage iPage = new Page(qPage.getOffset(), qPage.getLimit());
        QueryWrapper<Pet> queryWrapper=new QueryWrapper<>();
        if(obj.getPtype()!=null){
            queryWrapper.eq("ptype",obj.getPtype());
        }
        if(StringUtils.isNotBlank(obj.getPname())){
            queryWrapper.like("pname",obj.getPname());
        }
        queryWrapper.orderByAsc("ctime");
        iPage = this.baseMapper.selectPage(iPage,queryWrapper);
        return ResultView.ok(iPage);
    }

    @Override
    public ResultView add(Pet obj) {
        obj.setCtime(new Date());
        obj.setUtime(new Date());
        this.save(obj);
        return ResultView.ok();
    }

    @Override
    public ResultView del(String id) {
        this.removeById(id);
        return ResultView.ok();
    }

    @Override
    public ResultView up(Pet obj) {
        obj.setUtime(new Date());
        this.updateById(obj);
        return ResultView.ok();
    }


}
